package cn.edu.scau.cmi.chenyuanying.simpleFactoryMethod;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;

public class SimpleFactoryMethodController implements Initializable {
	@FXML Button showButton;
	@FXML ChoiceBox<String> choiceBox;
	@FXML TextArea showTextArea;
	private String nowMeat; 
	private final String[] chooseType = {"Please choose", "Pork", "Beef", "Chicken"};
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		ObservableList<String> list = FXCollections.observableArrayList(chooseType);
		nowMeat = chooseType[0];
		choiceBox.setItems(list);
		choiceBox.getSelectionModel().selectFirst();
		choiceBox.getSelectionModel().selectedIndexProperty().addListener(
				new ChangeListener<Number>() {
					@Override
					public void changed(ObservableValue<? extends Number> observable, Number oldValue,
							Number newValue) {
						nowMeat = chooseType[newValue.intValue()];
					}
				});
	}
	
	public void showMeat(ActionEvent e) {
		if(nowMeat.compareTo(chooseType[0]) == 0) {
			showTextArea.setText("");
			return;
		}
		Meat instance = MeatFactory.getMeatObj(nowMeat);
		showTextArea.setText(instance.toString());
	}

}
